package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;
    /*
    * 查询角色列表
    * */
    @GetMapping("/roles")
    public Result roleList(String name, String label,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10")  Integer pageSize){
        log.info("条件分页查询部门: name={}, label={}, page={}, pageSize={}", name, label, page, pageSize);
        PageResult<Role> pageResult = roleService.roleList(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    /*
     * 删除角色
     * */
    @Log
    @DeleteMapping("/roles/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色: {}" , id);
        roleService.deleteById(id);
        return Result.success();
    }

    /*
    * 添加角色
    * */
    @Log
    @PostMapping("roles")
    public Result add(@RequestBody Role role){
        log.info("角色: {}",role);
        roleService.add(role);
        return Result.success();
    }

    /*
    * 根据id查询角色
    * */
    @GetMapping("/roles/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查询id：{}",id);
        Role role = roleService.selectById(id);
        return Result.success(role);
    }
    /*
    * 修改角色
    * */
    @Log
    @PutMapping("/roles")
    public Result update(@RequestBody Role role){
        log.info("修改角色：{}",role);
        roleService.update(role);
        return Result.success();
    }
    /*
    * 查询所有角色
    * */
    @GetMapping("/roles/list")
    public Result selectAll(){
        log.info("查询所有角色");
        List<Role> roleList = roleService.selectAll();
        return Result.success(roleList);
    }

}
